package com.ap.core.jbehave.email;

import com.ap.core.cxf.email.jaxb.common.MessageStatusType;
import com.ap.core.cxf.email.jaxb.managemessage.DataMapInfoType;
import com.ap.core.cxf.email.jaxb.managemessage.SendEmail1Request;
import com.ap.core.cxf.email.jaxb.managemessage.SendEmail1Response;
import com.ap.core.cxf.email.jaxb.managemessage.TemplateNameType;
import com.ap.core.jbehave.StoryContext;
import com.ap.core.util.JaxbUtil;
import com.ap.wsdl.managemessageservice_1.Fault;
import com.ap.wsdl.managemessageservice_1.ManageMessage1PortType;
import org.hamcrest.CoreMatchers;
import org.jbehave.core.annotations.Given;
import org.jbehave.core.annotations.Then;
import org.jbehave.core.annotations.When;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import java.util.List;

import static org.hamcrest.CoreMatchers.equalTo;
import static org.hamcrest.MatcherAssert.assertThat;


/**
 * This class contains the steps used by the test harness JBehave instances.
 */
public class EmailSteps {

    private SendEmail1Response sendEmail1Response;
    private SendEmail1Request sendEmail1Request;
    protected final StoryContext storyContext;
    protected ClassPathXmlApplicationContext factory;

    protected final Logger LOG = LoggerFactory.getLogger(getClass());

    public EmailSteps(StoryContext storyContext) {
        this.storyContext = storyContext;
        factory = new ClassPathXmlApplicationContext("APCoreServicesTest.xml");
    }

    @Given("the OSB has submitted a structurally valid SENDEMAIL request to APCore")
    public void givenSENDEMAILRequestSubmitted() {
        sendEmail1Request = new SendEmail1Request();
        sendEmail1Request.setFrom("ranjit.singh@vocalink.com");
        sendEmail1Request.setTemplateName(TemplateNameType.WELCOMETOZAPP);

        List<String> toEmailAddresses = sendEmail1Request.getTo();
        toEmailAddresses.add("ranjit.singh@vocalink.com");

        List<String> ccEmailAddresses = sendEmail1Request.getCC();
        ccEmailAddresses.add("ranjit.singh@vocalink.com");

        List<String> bccEmailAddresses = sendEmail1Request.getBCC();
        bccEmailAddresses.add("ranjit.singh@vocalink.com");

        List<DataMapInfoType> dataMapInfoTypes = sendEmail1Request.getDataMap();
        DataMapInfoType dataMapInfoType = new DataMapInfoType();
        dataMapInfoType.setName("FI_NAME");
        dataMapInfoType.setValue("HSBC, Bank");
        dataMapInfoTypes.add(dataMapInfoType);

        DataMapInfoType uuid_dataMapInfoType = new DataMapInfoType();
        uuid_dataMapInfoType.setName("UU_ID");
        uuid_dataMapInfoType.setValue("sahdjkashjdhskjahgsfjh847621");

        dataMapInfoTypes.add(uuid_dataMapInfoType);

        DataMapInfoType title_dataMapInfoType = new DataMapInfoType();
        title_dataMapInfoType.setName("title");
        title_dataMapInfoType.setValue("Welcome to Zapp!");

        dataMapInfoTypes.add(title_dataMapInfoType);

        DataMapInfoType priority_dataMapInfoType = new DataMapInfoType();
        priority_dataMapInfoType.setName("high_priority");
        priority_dataMapInfoType.setValue("TRUE");

        dataMapInfoTypes.add(priority_dataMapInfoType);

    }

    @When("APCore receives the structurally valid SENDEMAIL request")
    public void whenAPCoreReceivedValidEMailRequest()throws Fault{
        JaxbUtil.newInstance().javaToXML(SendEmail1Request.class, sendEmail1Request);
        final ManageMessage1PortType manageMessage1PortType = factory.getBean(ManageMessage1PortType.class);

        sendEmail1Response = manageMessage1PortType.sendEmail1(sendEmail1Request);

        JaxbUtil.newInstance().javaToXML(SendEmail1Response.class, sendEmail1Response);
    }

    @Then("email is sent to To field as per template request")
    public void thenEmailSentToField(){
        assertThat("email response is null", true, CoreMatchers.is(equalTo(sendEmail1Response != null)));
        assertThat("email response is null", true, CoreMatchers.is(equalTo(sendEmail1Response.getMessageStatus().equals(MessageStatusType.RECD))));
    }


}



